from appium.webdriver.common.touch_action import TouchAction
from appium import webdriver
from time import sleep
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import os


class Base():
    def __init__(self):
        desired_capabilities={}
        desired_capabilities['platformName']='Android'
        desired_capabilities["platformVersion"]="7.1.2"
        desired_capabilities["deviceName"]="127.0.0.1:21503"
        desired_capabilities["appPackage"]="com.tal.kaoyan"
        desired_capabilities["appActivity"]=".ui.activity.SplashActivity"
        desired_capabilities["unicodeKeyboard"] = True
        desired_capabilities["resetKeyboard"] = True
        desired_capabilities["noReset"] = True
        driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_capabilities)
        x = driver.get_window_size()['width']
        y = driver.get_window_size()['height']
        self.driver = driver
        self.x = x
        self.y = y
    def swipe_left(self):
        x1 = 0.8 * self.x
        y1 = 0.5 * self.y
        x2 = 0.2 * self.x
        y2 = 0.5 * self.y
        self.driver.swipe(x1, y1, x2, y2)
    def swipe_right(self):
        x1 = 0.2 * self.x
        y1 = 0.5 * self.y
        x2 = 0.8 * self.x
        y2 = 0.5 * self.y
        self.driver.swipe(x1, y1, x2, y2)
    def swipe_up(self):
        x1 = 0.5 * self.x
        y1 = 0.8 * self.y
        x2 = 0.5 * self.x
        y2 = 0.2 * self.y
        self.driver.swipe(x1, y1, x2, y2)
    def swipe_down(self):
        x1 = 0.5 * self.x
        y1 = 0.2 * self.y
        x2 = 0.5 * self.x
        y2 = 0.8 * self.y
        self.driver.swipe(x1, y1, x2, y2)
    def find_element(self,locator):
        return WebDriverWait(self.driver, 5, 0.3).until(EC.visibility_of_element_located(locator=locator))
    def find_elements(self,locator):
        return WebDriverWait(self.driver, 5, 0.3).until(EC.visibility_of_all_elements_located(locator=locator))
    def send_keys(self,locator,body):
        self.find_element(locator=locator).clear()
        self.find_element(locator=locator).send_keys(body=body)
    def quit(self):
        self.driver.quit()

if __name__ == '__main__':
    Base()
